﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTSDAL.LINQSQL.DAO;
using UTSDAL.LINQSQL.Model;
using System.Xml;
using System.IO;
using System.Xml.Schema;
using System.Xml.Linq;
using System.Xml.Xsl;
using System.Globalization;
using UTSDAL.LINQSQL.Helper;
using System.Reflection;
using UTSDAL.LINQSQL.Views;
using System.Runtime.Serialization.Formatters.Binary;

namespace UTSConsole
{
    class Program
    {

        

        public static void serialize(Object obj, String fileName)
        {
            Stream stream = File.Open(fileName, FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();

            bformatter.Serialize(stream, obj);
            stream.Close();

        }

        public static Object deserialize(String fileName)
        {
            Stream stream = File.Open(fileName, FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();

            Object obj = formatter.Deserialize(stream);
            stream.Close();

            return obj;

        }

        public static void addTimetableEvent(String[] args)
        {
            UTSDataContext context = new UTSDataContext();

            TimetableEvent timetableEventToUpdate = new TimetableEvent();
            timetableEventToUpdate.TimetableEventId = Guid.NewGuid();


            var moduleVar = from m in context.Modules
                            where m.ModuleName.Equals("Distributed System")
                            select m;

            UTSDAL.LINQSQL.Model.Module module = moduleVar.SingleOrDefault<UTSDAL.LINQSQL.Model.Module>();
            timetableEventToUpdate.ModuleId = module.ModuleId;
            timetableEventToUpdate.ModuleEventId = module.ModuleEvents.ElementAt(0).ModuleEventId;

            
            timetableEventToUpdate.StartTime = TimeSpan.Parse("08:30");
            timetableEventToUpdate.EndTime = TimeSpan.Parse("10:30");

            timetableEventToUpdate.CourseDayId = context.CourseDays.ToList<CourseDay>().ElementAt(0).CourseDayId;
            timetableEventToUpdate.RoomId = context.Rooms.ToList<Room>().ElementAt(0).RoomId;
            timetableEventToUpdate.TimetableEventTypeId = new TimetableEventTypeDAO().getReservationEventType().TimetableEventTypeId;

            new TimetableEventDAO().insert(timetableEventToUpdate);


        }


        public static void MainTime(String[] args)
        {
            String startTime = "09:00";
            String endTime = "10:30";

            String tentativeTime = "10:30";


            TimeSpan startTimeSpan = TimeSpan.Parse(startTime);
            TimeSpan endTimeSpan = TimeSpan.Parse(endTime);

            TimeSpan tentativeTimeSpan = TimeSpan.Parse(tentativeTime);

            Console.WriteLine(tentativeTimeSpan > startTimeSpan && tentativeTimeSpan < endTimeSpan);

        }

        public static void RunClass(Assembly assembly, string className, string methodName)
        {
            // Create the assemblies from our current DLL.
            Assembly _Assemblies = assembly;
            // Get the type that we want from the assemblies.
            //  IE: This would be the fully qualified class name (including namespace)
            //  Example: "Reflectionism.Examples.Example1" or "Reflectionism.Examples.Example2"
            Type _Type = null;
            try
            {
                _Type = _Assemblies.GetType(className);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\nError - couldn't obtain classrd from " + className);
                Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
                return;
            }
            // Get the desired method we want from the target type.
            MethodInfo _MethodInfo = null;
            try
            {
                _MethodInfo = _Type.GetMethod(methodName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\nError - couldn't obtain method " +
                            methodName + " from " + className);
                Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
                return;
            }

            // The first parameter to pass into the Invoke Method coming up.
            Object _InvokeParam1 = Activator.CreateInstance(_Type);
            // This calls the target method ("DisplayMyself").
            //  NOTE: I'm not passing any arguments down to the method being invoked.
            //  Therefore, I'm passing null as my argument, otherwise Invoke takes an
            //  array of Objects.
            _MethodInfo.Invoke(_InvokeParam1, null);
        }

        static void TimetableMain(string[] args)
        {
            TimetableEventTypeDAO eventTypeDAO = new TimetableEventTypeDAO();
            TimetableEventDAO eventDAO = new TimetableEventDAO();
            LecturerDAO lecturerDAO = new LecturerDAO();
            CourseDayDAO courseDayDAO = new CourseDayDAO();


            UTSDataContext context = DataContextHelper.Context;

            TimetableEvent timetableEvent = new TimetableEvent();
            timetableEvent.CourseDay = new CourseDayDAO().selectAll().FirstOrDefault<CourseDay>();
            timetableEvent.Module = new ModuleDAO().selectAll().FirstOrDefault<UTSDAL.LINQSQL.Model.Module>();
            timetableEvent.ModuleEvent = new ModuleEventDAO().selectAll().FirstOrDefault<ModuleEvent>();
            timetableEvent.Room = new RoomDAO().selectAll().FirstOrDefault<Room>();
            timetableEvent.Semester = new SemesterDAO().selectAll().FirstOrDefault<Semester>();
            timetableEvent.Cohort = new CohortDAO().selectAll().FirstOrDefault<Cohort>();
            
            timetableEvent.TimetableEventType = new TimetableEventTypeDAO().selectAll().FirstOrDefault<TimetableEventType>();
            timetableEvent.StartTime = DateTime.Now.TimeOfDay;
            timetableEvent.EndTime = DateTime.Now.AddHours(3).TimeOfDay;

            timetableEvent.TimetableEventId = Guid.NewGuid();
            eventDAO.insert(timetableEvent);
         

         
            
        }
    }
}
